Attribute VB_Name = "modSpectrum"
Option Explicit

Public BufferOUT(520) As Byte

Public Type Packet_Out
    PID1 As Byte
    PID2 As Byte
    LEN As Integer  ' signed, but data payload always less than 32768
    EXPECTEDRESPONSE As Byte
    Data(514) As Byte
End Type

Public Type Spec
    Data() As Long  ' this keeps total of static data under 64K VB limit
    Channels As Integer
End Type

Public SPECTRUM As Spec

Public Function ShowStatusValueStrings(m_DP5_Status As Stat) As String
    Dim strConfig As String
    Dim strTemp As String
    Dim strIntPart As String
    Dim strFracPart As String
    
    strConfig = "Device Type: " & m_DP5_Status.strDeviceID & vbNewLine
    strTemp = "Serial Number: " & CStr(m_DP5_Status.SerialNumber) & vbNewLine 'SerialNumber
    strConfig = strConfig & strTemp
    strTemp = "Firmware: " + VersionToStr(m_DP5_Status.Firmware) & vbNewLine
    strConfig = strConfig & strTemp
    strTemp = "FPGA: " + VersionToStr(m_DP5_Status.FPGA) & vbNewLine
    strConfig = strConfig & strTemp
    strTemp = "Fast Count: " & CStr(CDbl(m_DP5_Status.FastCount)) & vbNewLine     'FastCount
    strConfig = strConfig & strTemp
    strTemp = "Slow Count: " & CStr(CDbl(m_DP5_Status.SlowCount)) & vbNewLine      'FastCount
    strConfig = strConfig & strTemp
    strTemp = "Accumulation Time: " & CStr(m_DP5_Status.AccumulationTime) '& vbNewLine       'AccumulationTime
    strConfig = strConfig & strTemp
    ShowStatusValueStrings = strConfig
End Function

Public Function ProcessSpectrumCfgForFile(strCfgIn As String, Dp5CmdList As Collection) As String
    Dim cstrRawCfgIn As String
    Dim cstrCmdD As String
    Dim cstrDisplayCfgOut As String
    Dim idxCfg As Long
    Dim varCmd As Variant
    Dim strSpectrumCfg As String
    ProcessSpectrumCfgForFile = ""
    cstrRawCfgIn = ""
    For idxCfg = 1 To Len(strCfgIn)
        cstrRawCfgIn = cstrRawCfgIn + Mid(strCfgIn, idxCfg, 1)
        If (Mid(strCfgIn, idxCfg, 1) = ";") Then
            cstrRawCfgIn = cstrRawCfgIn + vbNewLine
        End If
    Next
    cstrDisplayCfgOut = cstrRawCfgIn
    For Each varCmd In Dp5CmdList
        cstrCmdD = CStr(varCmd)
        If (Len(cstrCmdD) > 0) Then
            cstrDisplayCfgOut = AppendCmdDesc(cstrCmdD, cstrDisplayCfgOut)
        End If
    Next
    strSpectrumCfg = cstrDisplayCfgOut
    'remove a vbnewline to display correctly
    If (Right(strSpectrumCfg, 2) = vbNewLine) Then
        strSpectrumCfg = Left(strSpectrumCfg, Len(strSpectrumCfg) - 2)
    End If
    ProcessSpectrumCfgForFile = strSpectrumCfg
End Function

'converts vb type 24bit rgb (BlueGreenRed) to QBColor
Public Function RgbToQbColor(PlotColor As Long)
    Dim PlotColorIndex As Integer   'QBColors 0-15
    Select Case PlotColor
        Case &H0&: PlotColorIndex = 0   'Black
        Case &H800000: PlotColorIndex = 1    'Blue
        Case &H8000&: PlotColorIndex = 2    'Green
        Case &H808000: PlotColorIndex = 3    'Cyan
        Case &H80&: PlotColorIndex = 4    'Red
        Case &H800080: PlotColorIndex = 5    'Magenta
        Case &H8080&: PlotColorIndex = 6    'Brown
        Case &HC0C0C0: PlotColorIndex = 7    'White
        Case &H808080: PlotColorIndex = 8    'Grey
        Case &HFF0000: PlotColorIndex = 9    'Light Blue
        Case &HFF00&: PlotColorIndex = 10    'Light Green
        Case &HFFFF00: PlotColorIndex = 11    'Light Cyan
        Case &HFF&: PlotColorIndex = 12    'Light Red
        Case &HFF00FF: PlotColorIndex = 13    'Light Magenta
        Case &HFFFF&: PlotColorIndex = 14    'Yellow
        Case &HFFFFFF: PlotColorIndex = 15    'Bright White
        Case Else
            PlotColorIndex = &H0   'unknown color
    End Select
    PlotColorIndex = PlotColorIndex + 1
    If (PlotColorIndex >= 16) Then PlotColorIndex = 0
    RgbToQbColor = QBColor(PlotColorIndex)
End Function

Public Sub Plot_Spectrum(picMCAPlot As VB.PictureBox, SPECTRUM As Spec, isLinear As Boolean, isLines As Boolean)
    Dim X As Integer
    Dim Y As Integer

    Dim MCAMax As Long
    Dim MCAPeak As Long
    Dim TString As String
    Dim Buffer(64)
    Dim MCSScale As Long
    Dim MCAScale As Long
    
    Dim PlotXMin As Long
    Dim PlotXMax As Long
    
    Dim LogScaleValue As Long
    
    MCAMax = 0
    MCAPeak = 0
    For X = 0 To SPECTRUM.Channels - 1
        If SPECTRUM.Data(X) > MCAMax Then
            MCAMax = SPECTRUM.Data(X)
            MCAPeak = X
        End If
    Next X
    If isLinear Then
        MCAScale = Fix(MCAMax + 10 + (10# * Log(MCAMax + 1)))
    Else
        LogScaleValue = Fix(Log(MCAMax + 1) / Log(10) + 1)
        MCAScale = 10 ^ LogScaleValue
    End If
    
    PlotXMin = 0
    PlotXMax = SPECTRUM.Channels - 1
    
    If isLinear Then
        picMCAPlot.Scale (PlotXMin, MCAScale)-(PlotXMax + 1, 0)
        picMCAPlot.Line (PlotXMin, 0)-(PlotXMax + 1, MCAScale), picMCAPlot.BackColor, BF
    Else
        picMCAPlot.Scale (PlotXMin, Log(MCAScale))-(PlotXMax + 1, 0)
        picMCAPlot.Line (PlotXMin, 0)-(PlotXMax + 1, Log(MCAScale)), picMCAPlot.BackColor, BF
    End If

    If isLinear Then
        picMCAPlot.Line (PlotXMin, 0)-(PlotXMax + 1, MCAScale), picMCAPlot.BackColor, BF
        For X = 1 To 9
            picMCAPlot.Line (PlotXMin, MCAScale * X / 10)-(PlotXMax + 1, MCAScale * X / 10), vbWhite
        Next X
    Else
        picMCAPlot.Line (PlotXMin, 0)-(PlotXMax + 1, Log(MCAScale)), picMCAPlot.BackColor, BF
        For Y = 1 To LogScaleValue
            For X = 1 To 9
                picMCAPlot.Line (PlotXMin, Log(MCAScale * X / (10 ^ Y)))-(PlotXMax + 1, Log(MCAScale * X / (10 ^ Y))), colorLightGray
            Next X
        Next Y
    End If

    If isLines Then
        For X = PlotXMin To PlotXMax - 1
            If isLinear Then
                picMCAPlot.Line (X, SPECTRUM.Data(X))-(X + 1, SPECTRUM.Data(X + 1)), PlotColor
            Else
                picMCAPlot.Line (X, Log(1# + SPECTRUM.Data(X)))-(X + 1, Log(1# + SPECTRUM.Data(X + 1))), PlotColor
            End If
        Next X
    Else
        For X = PlotXMin To PlotXMax
            If isLinear Then
                picMCAPlot.Line (X, 0)-(X + 0.5, SPECTRUM.Data(X)), PlotColor, BF
            Else
                picMCAPlot.Line (X, 0)-(X + 0.5, Log(1# + SPECTRUM.Data(X))), PlotColor, BF
            End If
        Next X
    End If
End Sub

Public Sub Plot_Scope(picScope As VB.PictureBox, Scope() As Byte, PlotColor As Long)
    Dim X As Integer
    picScope.Scale (0, 255)-(2047, 0)
    picScope.Line (0, 0)-(2047, 255), picScope.BackColor, BF
    For X = 0 To 2046
        picScope.Line (X, Scope(X))-(X + 1, Scope(X + 1)), PlotColor
    Next X
End Sub

Public Sub WriteTextFile(strFilename As String, strData As String, Optional OverWrite As Boolean = False)
    Dim intFile As Integer
    
    intFile = FreeFile
    If (OverWrite) Then
        Open strFilename For Output As #intFile
    Else
        Open strFilename For Append As #intFile
    End If
    Print #intFile, strData
    Close #intFile
End Sub

Public Sub LogEvent(strEvent As String, Optional bOverWrite As Boolean = False)
    Dim FileNumber As Integer
    Dim strMsg As String
    strMsg = Now & "," & strEvent
    FileNumber = FreeFile
    If (bOverWrite) Then
        Open "EventLog.txt" For Output As #FileNumber
    Else
        Open "EventLog.txt" For Append As #FileNumber
    End If
    Print #FileNumber, strMsg
    Close #FileNumber
End Sub


